//
//  BattleWarshipItem.m
//  SpaceStorm
//
//  Created by  mmj on 13-7-13.
//  船舰详细信息(船旁边的信息框)
//

#import "BattleShipInfoItem.h"
#import "BattleScene.h"

@implementation BattleShipInfoItem

+(id) shipItemWidthShip:(Ship*)ship
{
    return [[[BattleShipInfoItem alloc] initItemWithShip:ship] autorelease];
}

-(id) initItemWithShip:(Ship*)ship
{
    if((self = [super init]))
    {
        //记录下舰船的引用
        _ship = ship;
        //初始化背景
        [self initBackGround];
        //初始化标签信息
        /* vo.maxArmoured = _ship.prop.maxShield;
         vo.maxHp = ship.prop.maxArmoured;
         vo.maxShield = ship.prop.maxHp;
         hujiaValue:(int) hujiaValue
         structValue:(int) structValue
         shipName:(NSString *) shipName*/
        [self initLabelInfo];
        
        [self updateLabelInfo:_ship.prop.maxShield hujiaValue:_ship.prop.maxArmoured structValue:_ship.prop.maxHp shipName:_ship.prop.name];
        
        [self scheduleUpdate];
    }
    return self;
}

/**
 UI背景
 */
-(void) initBackGround
{
    size = [[CCDirector sharedDirector] winSize];
    
    CCSprite* _buleLine2;
    //护盾
    CCSprite* _hudun;
    //护甲
    CCSprite* _hujia;
    //结构
    CCSprite* _structure;
    
    //线条1
    _buleLine1 = [CCSprite spriteWithSpriteFrameName:@"blueLine.png"];
    _buleLine2 = [CCSprite spriteWithSpriteFrameName:@"blueLine.png"];
    //护盾
    _hudun  = [CCSprite spriteWithSpriteFrameName:@"energyPro.png"];
    //护甲
    _hujia = [CCSprite spriteWithSpriteFrameName:@"armourPro.png"];
    //结构
    _structure = [CCSprite spriteWithSpriteFrameName:@"greenBlood.png"];
    
    _buleLine1.anchorPoint  = ccp(0, 0);
    _buleLine1.position = ccp(0,_buleLine1.contentSize.height/2);
    _buleLine1.rotation = -135;
    [self addChild:_buleLine1];
    
//    _buleLine2.anchorPoint = ccp(0, 0);
    _buleLine2.position = ccp(_buleLine2.contentSize.width/2, _buleLine2.contentSize.height/2);
    [self addChild:_buleLine2];
    //护盾
    _hudun.position = ccp(_hudun.contentSize.width/2+6, _hudun.contentSize.height/2+46);
    [self addChild:_hudun];
    //护甲
    _hujia.position = ccp(_hujia.contentSize.width/2+6, _hujia.contentSize.height/2+30);
    [self addChild:_hujia];
    //结构
    _structure.position = ccp(_structure.contentSize.width/2+6, _structure.contentSize.height/2+14);
    [self addChild:_structure];
    
    
}

/**
 UI 初始化文本信息
 */

-(void) updateLabelInfo:(int) hudunValue
             hujiaValue:(int) hujiaValue
            structValue:(int) structValue
               shipName:(NSString *) shipName
{

    //护盾
    _shield = [CCLabelTTF labelWithString:@"160000" fontName:@"Marker Felt" fontSize:10];
//    [_shield setString:@stringWithFormation:@"%d", 22];
//    [_shield setString:[NSString stringWithFormation:@"%d", 2002]];
    [_shield setString:[NSString stringWithFormat:@"%d", 2002]];
    //护甲
    
    _armoured = [CCLabelTTF labelWithString:@"160000" fontName:@"Marker Felt" fontSize:10];
    //    [_labelHujiaNum set]
    
    //结构
    _struct = [CCLabelTTF labelWithString:@"160000" fontName:@"Marker Felt" fontSize:10];
    
    
    //战舰类型
    _shipType = [CCLabelTTF labelWithString:@"神徒级战列舰" fontName:@"Marker Felt" fontSize:10];
    

    
}

/**
 UI 初始化文本信息
 */
-(void) initLabelInfo
{

    //护盾
    CCLabelTTF* _labelHudun;
    CCLabelTTF* _labelHujia;
    CCLabelTTF* _labelStructure;
    //护盾
    _labelHudun = [CCLabelTTF labelWithString:@"护盾 ：" fontName:@"Marker Felt" fontSize:10];
    _labelHudun.position = ccp(_labelHudun.contentSize.width/2+ 16, _labelHudun.contentSize.height/2+44);
    _labelHudun.color  = ccc3(255, 255, 255);
    [self addChild:_labelHudun];
    
    _shield = [CCLabelTTF labelWithString:@"160000" fontName:@"Marker Felt" fontSize:10];
    _shield.position = ccp(_shield.contentSize.width/2+ 54, _shield.contentSize.height/2+44);
    _shield.color  = ccc3(255, 255, 255);
    [self addChild:_shield];
    
    //护甲
    _labelHujia = [CCLabelTTF labelWithString:@"护甲 ：" fontName:@"Marker Felt" fontSize:10];
    _labelHujia.position = ccp(_labelHujia.contentSize.width/2+ 16, _labelHujia.contentSize.height/2+28);
    _labelHujia.color  = ccc3(255, 255, 255);
    [self addChild:_labelHujia];
    
    _armoured = [CCLabelTTF labelWithString:@"160000" fontName:@"Marker Felt" fontSize:10];
    _armoured.position = ccp(_armoured.contentSize.width/2+ 54, _armoured.contentSize.height/2+28);
    _armoured.color  = ccc3(255, 255, 255);
    [self addChild:_armoured];
    
    //结构
    _labelStructure = [CCLabelTTF labelWithString:@"结构 ：" fontName:@"Marker Felt" fontSize:10];
    _labelStructure.position = ccp(_labelStructure.contentSize.width/2+ 16, _labelStructure.contentSize.height/2+12);
    _labelStructure.color  = ccc3(255, 255, 255);
    [self addChild:_labelStructure];
    
    _struct = [CCLabelTTF labelWithString:@"160000" fontName:@"Marker Felt" fontSize:10];
    _struct.position = ccp(_struct.contentSize.width/2+ 54, _struct.contentSize.height/2+12);
    _struct.color  = ccc3(255, 255, 255);
    [self addChild:_struct];
    
    //战舰类型
    _shipType = [CCLabelTTF labelWithString:@"神徒级战列舰" fontName:@"Marker Felt" fontSize:10];
    _shipType.position = ccp(_shipType.contentSize.width/2+ 10, _shipType.contentSize.height/2-20);
    _shipType.color  = ccc3(255, 255, 255);
    [self addChild:_shipType];

}

-(void) update:(ccTime)delta
{
    //更新位置
    [self updateItemPosition];
    
    //更新角度
//    [self updateItemRotation];
}

/**
	更新信息面板的角度
 */
-(void) updateItemRotation
{
    self.rotation = - _ship.rotation;
}

/**
	更新信息面板的位置
    船头第一象限方向时，右下
    船头第二象限方向时，右上
    船头第三象限方向时，右下
    船头第四象限方向时，右上
 */
-(void) updateItemPosition
{
    CGPoint v = ccpForAngle(CC_DEGREES_TO_RADIANS(_ship.rotation));
    if(v.x * v.y >= 0)//一三象限
    {
        CGPoint newP = ccpMult(ccpForAngle(CC_DEGREES_TO_RADIANS(-45)), _buleLine1.contentSize.width);
        float x = (int)newP.x + size.width / 2;
        float y = (int)newP.y + size.height / 2;
        self.position = ccp(x, y);
        _buleLine1.rotation = -135;
    }
    else//二四象限
    {
        CGPoint newP = ccpMult(ccpForAngle(CC_DEGREES_TO_RADIANS(45)), _buleLine1.contentSize.width);
        float x = (int)newP.x + size.width / 2;
        float y = (int)newP.y + size.height / 2;
        self.position = ccp(x, y);
        _buleLine1.rotation = 135;
    }
}

- (void) dealloc
{
    [super dealloc];
}

@end
